<?php

if (!defined("EXPONENT")) exit("");
if (!defined('SYS_FILES')) require_once(BASE.'subsystems/files.php');
include(BASE . "subsystems/getid3/mp3.php");
// get album config

	$music = null;
	if (isset($_REQUEST['album_id']))
	 $_REQUEST['album_id']=intval($_REQUEST['album_id']);
	else
	 $_REQUEST['album_id']=0;
	 
	if (isset($_POST['id'])) {
		$music = $db->selectObject('musics', 'id='.$_POST['id']);
	}
	else
	{
	 $music->rank = $db->max('musics', 'rank', "" , "album_id=".$_REQUEST['album_id']);
		if ($music->rank == null) {
			$music->rank = 0;
		} else { 
			$music->rank += 1;
		}
  }
  
		/*
		  7/28/2009
		  Immanuel192 change to support upload via ajax
		*/			
		$music = musics::update($_POST, $music);
		// 8/19/2009
    // Immanuel192: using getID3 to get timelength of media file and some other information
    if ($music->high_quality > 0)
    {
      $file=$db->selectObject('file', 'id='.$music->high_quality);
      $path= $file->directory.'/'.$file->filename;
		  if (file_exists($path))
				{
				  $f=new mp3file($path);
          $temp=$f->get_metadata();
          if (isset($temp['Length']))
            $music->length=$temp['Length'];				  
          if (isset($temp['Bitrate']))
            $music->bitrate=$temp['Bitrate'];
				 }
		}
		// 8/19/2009
		if (isset($music->id)) {
			$db->updateObject($music,"musics");
		} else {
			$db->insertObject($music,"musics");
		}		
		exponent_flow_redirect();
?>
